package com.iking.housekeeping.service.elderly;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iking.housekeeping.dao.elderly.ElderlyMapper;
import com.iking.housekeeping.dao.servicetype.ServiceTypeMapper;
import com.iking.housekeeping.entity.elderly.Elderly;
import com.iking.housekeeping.entity.servicetype.ServiceType;
import com.iking.housekeeping.httpbean.elderlyrelation.HttpElderlyRelation;
import com.iking.housekeeping.httpbean.elderlyreq.ElderlySelectReq;
import com.iking.housekeeping.httpbean.elderlyreq.HttpElderlyReq;
import com.iking.housekeeping.httpbean.elderlyres.ElderlyRes;
import com.iking.housekeeping.httpbean.elderlyres.HttpElderlyRes;
import com.iking.housekeeping.httpbean.list.Gender;
import com.iking.housekeeping.httpbean.servicestaff.reqbean.HttpGiveElderly;
import com.iking.housekeeping.util.token.WebTokenUtil;

/**
 * 老人管理
 * 
 * @author: gf
 * @date: 2018年4月18日 下午1:33:41
 */
@Service
public class ElderlyService {
	
	/**
	 * 老人管理
	 */
	@Autowired
	private ElderlyMapper elderlyMapper;	
	
	@Autowired
	private ServiceTypeMapper serviceTypeMapper;	//服务类型
	
	/**
	 * 	添加老人
	 *  @param req
	 *  @return int
	 *  @author gf
	 */
	public int insert(Elderly req) {
		return elderlyMapper.insertSelective(req);
	}
	
	/**
	 * 	删除老人
	 *  @param fId
	 *  @return int
	 *  @author gf
	 */
	public int delete(String fId) {

		return elderlyMapper.updateByFidPrimaryKey(fId);
	}
	
	/**
	 * 	批量删除老人
	 *  @param list
	 *  @return int
	 *  @author gf
	 */
	public int deleteBatch(List<String> list) {
		return elderlyMapper.deleteBatch(list);
	}
	
	/**
	 * 	更新老人信息
	 *  @param req
	 *  @return int
	 *  @author gf
	 */
	public int update(Elderly req) {
		return elderlyMapper.updateByPrimaryKeySelective(req);
	}

	/**
	 * 	通过身份证查询
	 *  @param fCardId
	 *  @return Elderly
	 *  @author gf
	 */
	public Elderly selectByCardId(String fCardId) {
		return elderlyMapper.selectByCardId(fCardId);
	}
	
	/**
	 * 	通过电话查询老人
	 *  @param fPhone
	 *  @return Elderly
	 *  @author gf
	 */
	public Elderly selectByPhone(String fPhone) {
		return elderlyMapper.selectByPhone(fPhone);
	}
	
	/**
	 * 	通过主键查询
	 *  @param fId
	 *  @return Elderly
	 *  @author gf
	 */
	public Elderly selectByParmaryKey(String fId) {
		return elderlyMapper.selectByPrimaryKey(fId);
	}
	
	/**
	 * 	查询所有老人（响应）
	 *  @param page
	 *  @return Page<HttpElderlyRes>
	 *  @author gf
	 */
	public Page<HttpElderlyRes> selectAll(ElderlySelectReq elderlyPage){
		//设置当前页和分页大小
		PageHelper.startPage(elderlyPage.getPageCurrent(), elderlyPage.getPageSize());
		//查询所有

		return elderlyMapper.selectAll(elderlyPage);
	}
	
	/**
	 * 	性别列表
	 *  @return List<Gender>
	 *  @author gf
	 */
	public List<Gender> selectGender() {
		return elderlyMapper.selectGender();
	}
	/**
	 * 
	 * @Description:查询所服务的老人信息
	 * @author:CGZ
	 * @param userId
	 * @return
	 */
	public  List<Elderly> getInformation(String fServiceStaffId) {
		return  elderlyMapper.selectByFserviceStaffId(fServiceStaffId);
		
	}
	
	/**
	 * 	通过老人查询家属(详情)
	 *  @param fElderly
	 *  @return List<ElderlyRelation>
	 *  @author gf
	 */
	public HttpElderlyReq selectByEldId(String fElderly){
		//通过老人查询家属
		List<HttpElderlyRelation> relationList = elderlyMapper.selectRelationByElderly(fElderly);
		//获取老人详情对象
		HttpElderlyReq elderlyRelation = elderlyMapper.selectElderlyDetails(fElderly);
		String getfServiceContent = elderlyRelation.getfContentId();
		if(getfServiceContent!=null &&  !"".equals(getfServiceContent) ){
			String[] split = getfServiceContent.split(",");
			//存放服务类型名称的
			String allTypeName ="";
			for (String string : split) {
				if(string == null || "".equals(string)) {
					continue;
				}
				ServiceType type = serviceTypeMapper.selectByPrimaryKey(string);
				String typeName = type.getfName();
				allTypeName += ( " " + typeName );
			}
			//服务内容
			elderlyRelation.setfContent(allTypeName);
		}
		elderlyRelation.setList(relationList);
		return elderlyRelation;
	}
	
	/**
	 * 	老人详情
	 *  @param fElderly
	 *  @return HttpElderlyReq
	 *  @author gf
	 */
	public HttpElderlyReq selectElderlyDetails(String fElderly) {
		
		return elderlyMapper.selectElderlyDetails(fElderly);
	}
	
	/**
	 * 	修改老人
	 *  @param httpGiveElderly
	 *  @return int
	 *  @author gf
	 */
	public int giveServiceStaff(HttpGiveElderly httpGiveElderly) {
		if(httpGiveElderly.getfElderlyId().size() > 0){
			return elderlyMapper.updateByStaffId(httpGiveElderly);
		}
		return 0;
	}

	/**
	 * 秦莆杰
	 * 查询所有的预约老人信息
	 * @return
	 */
	public List<ElderlyRes> selectAllElderly(HttpServletRequest request){
		String fServiceStsff = WebTokenUtil.getUserIdByRequest(request);
		return elderlyMapper.selectAllElderly(fServiceStsff);
	}
	
	/**
	 * 	通过老人id查询
	 *  @param fId
	 *  @return Elderly
	 *  @author gf
	 */
	public Elderly selectByPrimaryKey(String fId) {
		return elderlyMapper.selectByPrimaryKey(fId);
	}
	
	public Elderly selectElderlyByFId(String fId){
		return elderlyMapper.selectAllElderlys(fId);
	}

	/**
	 * 查询某种服务状态下的老人列表
	 * @param list
	 * @param fState
	 * @return
	 */
	public List<String> selectElderlyBySelectList(List<String> list, String fState) {
		return elderlyMapper.selectElderlyBySelectList(list, fState);
	}
	/**
	 * 查询老人是否被预约
	 * @param fId
	 * @return
	 */
	public List<String>  selectElderlyState( List<String> fId) {
		return elderlyMapper.selectElderlyState(fId);
	}

	/**
	 * 根据服务人员ID查询老人列表(未删除)
	 * @param list
	 * @return
	 */
	public List<String> selectByServiceStaffList(List<String> list) {
		return elderlyMapper.selectByServiceStaffList(list);
	}

	/**
	 * 查询老人在某时间段的服务信息
	 * @param httpElderlyRes
	 * @return
	 */
	public List<HttpElderlyRes> selectInfoByTime(HttpElderlyRes httpElderlyRes){
		return elderlyMapper.selectInfoByTime(httpElderlyRes);
	}
}
